<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>样式控制</title>
    <style>
        body {
            font-family: "Lato", "Helvetica Neue", Helvetica, Arial, sans-serif;
            color: #34495e;
        }

        .default {
            background: red;
            width: 100px;
            height: 100px;
        }

        .change {
            background: blue;
            width: 100px;
            height: 100px;
        }

        #block1,
        #block2 {
            margin: 20px 0;
        }
    </style>
    <script src="https://unpkg.com/san@latest/dist/san.dev.js"></script>
</head>

<body>
    <h1>样式控制</h1>
    <div id="app"></div>
    <script>
        var MyApp = san.defineComponent({
            template: `
            <div>
                <div id="block1" class="{{flag ? 'default' : 'change'}}"></div>
                <div id="block2" style="background: {{flag ? 'red' : 'blue'}};width:100px;height:100px;"></div>
                <button on-click="changeStyle" type="button">改变样式</button>
            </div>
            `,
            initData: function () {
                return {
                    flag: true
                }
            },
            changeStyle: function () {
                var flag = this.data.get('flag');
                this.data.set('flag', !flag);
            }
        });
        var myApp = new MyApp();
        myApp.attach(document.getElementById("app"));
    </script>
</body>

</html>